package soccer.ui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import soccer.domain.Klasemen;
import soccer.domain.Klub;

public class AplikasiKlasemen {
	private static Klasemen klasemen = new Klasemen();
	
	
	public static void main(String[] args)throws Exception {
		inisialisasi();
		
		displayKlasemen();
		List<Klub> daftarKlub = klasemen.urutkanBerdasarkanNama();
		System.out.println("Example: ");
		System.out.println("0. (Exit)");
		for(Integer i=0 ; i<daftarKlub.size() ; i++){
			System.out.println(i+1 + ". " + daftarKlub.get(i).getNama());
		}

		System.out.print("Choose the first Team: ");
		BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
		Integer klub1 = Integer.parseInt(br1.readLine());
		
		System.out.print("Choose the second Team: ");
		BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
		Integer klub2 = Integer.parseInt(br2.readLine());
		
		System.out.print("Enter the match result " + 
				daftarKlub.get(klub1-1).getNama() + " vs " +
				daftarKlub.get(klub2-1).getNama() + ": ");
		BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
		String skor = br3.readLine();
		Integer goal1 = Integer.parseInt(skor.substring(0, skor.indexOf("-")));
		Integer goal2 = Integer.parseInt(skor.substring(skor.indexOf("-")+1, skor.length()));
		
		
		
		List<Klub> terakhirMain = new ArrayList<Klub>();
		
		terakhirMain.add(daftarKlub.get(klub1-1));
		terakhirMain.add(daftarKlub.get(klub2-1));
		
		klasemen.setTerakhirMain(terakhirMain);
		terimaInput(klub1, klub2, goal1, goal2);
	}

	private static void terimaInput(Integer klub1, Integer klub2, Integer goal1, Integer goal2)throws Exception {
		// baca input ke variabel 
		
		// update klasemen dengan method input
		klasemen.inputMatch(klub1, klub2, goal1, goal2);
		
		displayKlasemen();
	}

	private static void displayKlasemen()throws Exception {
		
		List<Klub> table = klasemen.urutkanBerdasarkanPoin();
		List<Klub> terakhirMain = klasemen.getTerakhirMain();
		Integer i = 0;
		// looping dan tampilkan tabel klasemen		
		System.out.println("Klub\t\tMatch\tWin\tDraw\tLose\tIn-Out\tPoint");
		for(Object a : table){
			System.out.print(a);
			if(!terakhirMain.isEmpty()){
				if(terakhirMain.get(0).equals(a) || terakhirMain.get(1).equals(a))System.out.print("*\n");
				else System.out.print("\n");
			}
			else System.out.print("\n");
			i++;
		}
		
		List<Klub> match = new ArrayList<Klub>();
//		match.add(klasemen.getDatabase().get(klub1));
//		match.add(klasemen.getDatabase().get(klub2));
//		klasemen.setTerakhirMain(match);
		// klub yang terdaftar di terakhirMain dikasi bintang
		//klasemen.inputMatch(klub1, klub2, goal1, goal2);
//		System.out.println(goal1 + goal2);
	}

	private static void inisialisasi() {
		// buat klub
		Map<String, Klub> map = new HashMap<String, Klub>();
		
		Klub manUtd = new Klub();
		manUtd.setNama("Man Utd");
		manUtd.setMatch(17);
		manUtd.setWin(12);
		manUtd.setDraw(3);
		manUtd.setLose(2);
		manUtd.setGoalsIn(55);
		manUtd.setGoalsOut(18);
		manUtd.setPoint(39);
		
		Klub chelsea = new Klub();
		chelsea.setNama("Chelsea");
		chelsea.setMatch(17);
		chelsea.setWin(10);
		chelsea.setDraw(5);
		chelsea.setLose(2);
		chelsea.setGoalsIn(45);
		chelsea.setGoalsOut(28);
		chelsea.setPoint(35);
		
		Klub arsenal = new Klub();
		arsenal.setNama("Arsenal");
		arsenal.setMatch(17);
		arsenal.setWin(8);
		arsenal.setDraw(4);
		arsenal.setLose(5);
		arsenal.setGoalsIn(42);
		arsenal.setGoalsOut(18);
		arsenal.setPoint(36);
		
		Klub pool = new Klub();
		pool.setNama("Liverpool");
		pool.setMatch(17);
		pool.setWin(8);
		pool.setDraw(4);
		pool.setLose(5);
		pool.setGoalsIn(42);
		pool.setGoalsOut(18);
		pool.setPoint(36);
		
		map.put("1", manUtd);
		map.put("2", chelsea);
		map.put("3", arsenal);
		map.put("4", pool);
		// masukkan ke klasemen
		klasemen.setDatabase(map);
	}
}
